package com.geek.factory.controller;

import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.dto.ApproveDTO;
import com.geek.factory.entity.DeviceRepairApprovalLog;
import com.geek.factory.entity.DeviceRepairReport;
import com.geek.factory.result.Result;
import com.geek.factory.service.DeviceRepairApprovalLogService;
import com.geek.factory.service.DeviceRepairReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/repair/approval")
public class DeviceRepairApprovalLogController {

    @Autowired
    private DeviceRepairReportService reportService;
    @Autowired
    private DeviceRepairApprovalLogService approvalLogService;

    @PostMapping("/approve")
    public Result approve(@RequestBody ApproveDTO dto) {
        // 校验报修单状态
        DeviceRepairReport report = reportService.getById(dto.getReportId());
        if (report == null) {
            return new Result(SuccessConstant.FAIL, "报修单不存在");
        }
        if (!"pending".equalsIgnoreCase(report.getStatus())) {
            return new Result(SuccessConstant.FAIL, "该报修单已审批，不能重复审批");
        }

        // 写入审批日志
        DeviceRepairApprovalLog log = new DeviceRepairApprovalLog();
        log.setReportId(dto.getReportId());
        log.setApprover(dto.getApprover());
        log.setComment(dto.getComment());
        log.setDecision(dto.getDecision());
        log.setApprovalTime(LocalDateTime.now());

        boolean saved = approvalLogService.save(log);

        // 更新报修单状态
        String status = "approved".equalsIgnoreCase(dto.getDecision()) ? "approved" : "rejected";
        boolean updated = reportService.updateStatus(dto.getReportId(), status);

        return (saved && updated)
                ? new Result(SuccessConstant.SUCCESS, "审批完成")
                : new Result(SuccessConstant.FAIL, "审批失败");
    }


    @GetMapping("/list")
    public Result listAll() {
        List<DeviceRepairApprovalLog> list = approvalLogService.list();
        return new Result(SuccessConstant.SUCCESS, "查询成功", list);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        DeviceRepairApprovalLog log = approvalLogService.getById(id);
        return log != null
                ? new Result(SuccessConstant.SUCCESS, "查询成功", log)
                : new Result(SuccessConstant.FAIL, "记录不存在");
    }

    @PostMapping("/add")
    public Result add(@RequestBody DeviceRepairApprovalLog log) {
        boolean saved = approvalLogService.save(log);
        return saved
                ? new Result(SuccessConstant.SUCCESS, "新增成功")
                : new Result(SuccessConstant.FAIL, "新增失败");
    }

    @PutMapping("/update")
    public Result update(@RequestBody DeviceRepairApprovalLog log) {
        boolean updated = approvalLogService.updateById(log);
        return updated
                ? new Result(SuccessConstant.SUCCESS, "更新成功")
                : new Result(SuccessConstant.FAIL, "更新失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean deleted = approvalLogService.removeById(id);
        return deleted
                ? new Result(SuccessConstant.SUCCESS, "删除成功")
                : new Result(SuccessConstant.FAIL, "删除失败");
    }



}
